<?php
/*
==========================================
* 插件后台程序
==========================================
* Copyright (c) 2012 伯仁网络 All rights reserved.
* Author: YinHaiLin
* $Id: Article.class.php 10 2014-01-20 09:53:57Z XiongYong $
==========================================
*/

class Article extends PublicMod {

	/* 分类最大深度（最多允许创建多少级分类） */
	private $MaxSortGrade = 1;

	/* 聚合标签 */
	private $TagsArray = array(
		//'1' => '品牌',
		//'2' => '爆款',
		//'3' => '新品',
	);

  /* 判断是否需要文章图片("0"为不添加，"1"为添加) */
  static $PicJudge = '0';

	/* 系统内置分类ID */
	static $SysClassId = '1';

	/* 文章分类列表 */
    public function SortList() {
		parent::GetGlobal();  //调用公共方法
		parent::CheckLogin();  //检查登录
		parent::CheckPurview('single');  //权限判断

		//获取分类
        $list = parent::GetSortList(self::$dataArticleSort, 0, $this->MaxSortGrade);
        $list = parent::SetSortList($list);
        $this->Assign('list', $list); //数据循环变量
		//系统内置分类
		$this->Assign('sysClassId', self::$SysClassId);

        $this->Display();
    }

	/* 文章分类排序 */
	public function SortSequence() {
		parent::GetGlobal();  //调用公共方法
		parent::CheckLogin();  //检查登录
		parent::CheckPurview('article');  //权限判断

		$objArticleSort = M(self::$dataArticleSort);

		if ($_POST) {
			$sequence = deep_htmlspecialchars($_POST['sequence']);
			foreach ($sequence as $key => $value) {
				$data['sequence'] = get_between($value);
				$objArticleSort->where('`id` = '.intval($key))->Save($data);
			}
		}

		$backUrl = urldecode($_GET['url']);
		$this->Success('操作成功！', $backUrl);
	}

	/* 文章分类编辑 */
	public function SortEdit() {
		parent::GetGlobal();  //调用公共方法
		parent::CheckLogin();  //检查登录
		parent::CheckPurview('article');  //权限判断

		$objArticleSort = M(self::$dataArticleSort);

		if ($_POST) {
			$POST = deep_htmlspecialchars($_POST);
			$id = intval($POST['id']);
			parent::CheckField('sort_name', $POST['sort_name'], 'require', '分类名称不能为空');
			$data['sort_name'] = $POST['sort_name'];
			if ($POST['parent_id'] != '' ) { $data['parent_id'] = $POST['parent_id'];}
			$data['status'] = $POST['status'] ? '1' : '0';
			$data['sequence'] = get_between($POST['sequence']);
			$objArticleSort->Where('`id` ='.$id)->Save($data);

			$backUrl = urldecode($_GET['url']);
			$this->Success('操作成功！', $backUrl);
		} else {
			$id = intval($_GET['id']);
			$data = $objArticleSort->Where('`id` = '.$id)->Find();
			$data = deep_htmlspecialchars_decode($data);
			$this->Assign('data', $data);

			//获取可供选择的分类
			$where = '`id` <>'.$id;
			$sortList = parent::GetSortList(self::$dataArticleSort, 0, $this->MaxSortGrade-1, $where);
			$sortList = parent::SetSortList($sortList);
			$this->Assign('sortList', $sortList);

			//系统内置分类
			$this->Assign('sysClassId', self::$SysClassId);
	
			$this->Display();
		}
	}

	/* 文章分类添加 */
	public function SortAdd() {
		parent::GetGlobal();  //调用公共方法
		parent::CheckLogin();  //检查登录
		parent::CheckPurview('article');  //权限判断
		
		$objArticleSort = M(self::$dataArticleSort);

		if ($_POST) {
			$POST = deep_htmlspecialchars($_POST);
			parent::CheckField('sort_name', $POST['sort_name'], 'require', '分类名称不能为空');
			$data['sort_name'] = $POST['sort_name'];
			$data['parent_id'] = $POST['parent_id'];
			$data['status'] = $POST['status'] ? '1' : '0';
			$data['sequence'] = get_between($POST['sequence']);
			$objArticleSort->Add($data);

			$backUrl = urldecode($_GET['url']);
			$this->Success('操作成功！', $backUrl);
		} else {
			//获取可供选择的分类
			$sortList = parent::GetSortList(self::$dataArticleSort, 0,$this->MaxSortGrade-1);
			$sortList = parent::SetSortList($sortList);
			$this->Assign('sortList', $sortList);
	
			$this->Display();
		}
	}

	/* 文章分类删除 */
	public function SortDelete() {
		parent::GetGlobal();  //调用公共方法
		parent::CheckLogin();  //检查登录
		parent::CheckPurview('article');  //权限判断

		$objArticle = M(self::$dataArticle);
		$objArticleSort = M(self::$dataArticleSort);
		$id = intval($_GET['id']);
		if ($id != 0) {
			//将分类下数据放入"未分类"中
			$data['sort_id'] = -1;
			$where = '`sort_id` in ('.parent::GetIdList(self::$dataArticleSort, $id).')';
			$objArticle->Where($where)->Save($data);

			$idList = parent::GetIdList(self::$dataArticleSort, $id);
			//删除分类
			$where = '`id` in ('.$idList.')';
			$objArticleSort->Where($where)->delete();
		}

		$backUrl = urldecode($_GET['url']);
		$this->Success('操作成功！', $backUrl);
	}

	/* 文章列表 */
	public function ArticleList() {
		parent::GetGlobal();  //调用公共方法
		parent::CheckLogin();  //检查登录
		parent::CheckPurview('article');  //权限判断

		$objArticle = M(self::$dataArticle);
		//输出聚合标签信息
		$this->Assign('tags', $this->TagsArray);

		// 输出文章分类信息
		$sortList = parent::GetSortList(self::$dataArticleSort, 0, $this->MaxSortGrade);
		$sortList = parent::SetSortList($sortList);
		$this->Assign('sortList', $sortList);

		// 输出文章信息
		$GET = deep_htmlspecialchars($_GET);
		$sort_id =intval($GET['sort_id']);
		//分类筛选
		$whereArray[] = $sort_id !== 0 ? 'd.`sort_id` in ('.parent::GetIdList(self::$dataArticleSort, $sort_id).')' : '';
		//关键字筛选
		$whereArray[] = isset($GET['keywords']) ? 'INSTR(d.`article_name`, "'.$GET['keywords'].'")>0' : '';
		//标签筛选功能
		if (count($this->TagsArray) > 0) {
			$this->Assign('statusList', $this->TagsArray);
			$status = intval($GET['status']);
			$whereArray[] = $status !== 0 ? 'INSTR(d.`tags`, "|'.$status.',")>0' : '';
		}
		$whereArray = array_filter($whereArray);
		$where = implode(' AND ', $whereArray);
        $count = $objArticle->Alias('AS d')->Where($where)->Count();  //计算总数
        $p = new HyPage($count, 20);
        $list = $objArticle->Alias('AS d')->Field('d.*, s.`sort_name`')->Join(get_real_table(self::$dataArticleSort).' AS s ON d.sort_id = s.id')->Limit($p->FirstRow . ',' . $p->ListRow)->Where($where)->Order('d.`sequence` asc, d.`id` desc')->Select();
		$list = deep_htmlspecialchars_decode($list);
		//解析聚合标签
		foreach ($list as $key => $value) {
			$list[$key]['tags'] = format_tags($value['tags'], $this->TagsArray);
		}
        $page = $p->Show();            //分页的导航条的输出变量
        $this->Assign('page', $page);
        $this->Assign('list', $list); //数据循环变量

        $this->Display();
	}

	/* 文章编辑 */
	public function ArticleEdit() {
		parent::GetGlobal();  //调用公共方法
		parent::CheckLogin();  //检查登录
		parent::CheckPurview('article');  //权限判断

		$objArticle = M(self::$dataArticle);

		if ($_POST) {
			$POST = deep_htmlspecialchars($_POST);
			$id = intval($POST['id']);
			parent::CheckField('article_name', $POST['article_name'], 'require', '文章名称不能为空');
			//parent::CheckField('article_pic', $POST['article_pic'], 'require', '请上传文章图片');
			//程序处理插槽
			$rabbet = rabbet('articleadd_func', 'articleadd_deal', $POST);
			parent::CheckField('content', $POST['content'], 'require', '文章内容不能为空');
			$data['article_name'] = $POST['article_name'];
			$data['sort_id'] = $POST['sort_id'];
			$data['status'] = $POST['status'] ? '1' : '0';
			$data['article_pic'] = $POST['article_pic'];
			$data['content'] = $POST['content'];
			$data['tags'] = set_config($POST['tags']);
			if (is_array($rabbet)) { $data = array_merge($data, $rabbet);}
			$objArticle->Where('`id` ='.$id)->Save($data);

			$backUrl = urldecode($_GET['url']);
			$this->Success('操作成功！', $backUrl);
		} else {
			$id = intval($_GET['id']);
			$data = $objArticle->Where('`id` = '.$id)->Find();
			$data = deep_htmlspecialchars_decode($data);

			//程序处理插槽
			$rabbet = rabbet('articleadd_func', 'articleadd_show', $POST);

			//解析聚合标签
			$data['tags'] = format_tags($data['tags'], $this->TagsArray);
			$this->Assign('data', $data);

      //判断是否添加图片
      $this->Assign('picJudge', self::$PicJudge);

			//获取可供选择的分类
			$sortList = parent::GetSortList(self::$dataArticleSort, 0, $this->MaxSortGrade);
			$sortList = parent::SetSortList($sortList);
			$this->Assign('sortList', $sortList);

			$this->Display();
		}
	}

	/* 文章添加 */
	public function ArticleAdd() {
		parent::GetGlobal();  //调用公共方法
		parent::CheckLogin();  //检查登录
		parent::CheckPurview('article');  //权限判断

		$objArticle = M(self::$dataArticle);

		if ($_POST) {
			$POST = deep_htmlspecialchars($_POST);
			parent::CheckField('article_name', $POST['article_name'], 'require', '文章名称不能为空');
			//parent::CheckField('article_pic', $POST['article_pic'], 'require', '请上传文章图片');
			//程序处理插槽
			$rabbet = rabbet('articleadd_func', 'articleadd_deal', $POST);
			parent::CheckField('content', $POST['content'], 'require', '文章内容不能为空');
			$data['article_name'] = $POST['article_name'];
			$data['sort_id'] = $POST['sort_id'];
			$data['status'] = $POST['status'] ? '1' : '0';
			$data['article_pic'] = $POST['article_pic'];
			$data['content'] = $POST['content'];
			$data['tags'] = set_config($POST['tags']);
			$data['add_time'] = mktime();
			if (is_array($rabbet)) { $data = array_merge($data, $rabbet);}
			$objArticle->Add($data);

			$backUrl = urldecode($_GET['url']);
			$this->Success('操作成功！', $backUrl);
		} else {
			//程序处理插槽
			$rabbet = rabbet('articleadd_func', 'articleadd_show', $POST);

			//解析聚合标签
			$data['tags'] = format_tags('', $this->TagsArray);
			$this->Assign('data', $data);

      //判断是否添加图片
      $this->Assign('picJudge', self::$PicJudge);
	
			//获取可供选择的分类
			$sortList = parent::GetSortList(self::$dataArticleSort, 0, $this->MaxSortGrade);
			$sortList = parent::SetSortList($sortList);
			$this->Assign('sortList', $sortList);

			$this->Display();
		}
	}

	/* 文章删除 */
	public function ArticleDelete() {
		parent::GetGlobal();  //调用公共方法
		parent::CheckLogin();  //检查登录
		parent::CheckPurview('article');  //权限判断

		$objArticle = M(self::$dataArticle);

		$idList = deep_htmlspecialchars(implode(',', $_POST['id']));
		if ($_POST) {
			$objArticle->Where('`id` in ('.$idList.')')->Delete();
		}

		$backUrl = urldecode($_GET['url']);
		$this->Success('操作成功！', $backUrl);
	}
}